import { Component, OnInit } from '@angular/core';
import { BladeContainerBase } from "../../../../shared/blade/blade-container-base";
import { DialogResult } from "../../../../shared/dialog.service";
import { Observable } from "rxjs/Observable";
import { Command } from "../../../../shared/toolbar.service";
import { Blade } from "../../../../shared/blade/blade.model";
import { BladeData } from "../../../../shared/blade/blade-data";
import { StationTag } from "../../../../entity/station-tag.model";
import { StationTagService } from "../../../../service/station-tag.service";
import { StationTagEditComponent } from "../station-tag-edit/station-tag-edit.component";
import {Farm} from '../../../../entity/farm.model';

@Component({
    selector: 'app-station-tag-list',
    templateUrl: './station-tag-list.component.html',
    styleUrls: ['./station-tag-list.component.css']
})
export class StationTagListComponent extends BladeContainerBase<StationTag> {

	/**
	 * 关口表数据集合
	 * Creates an instance of StationTagListComponent.
	 * @memberof StationTagListComponent
	 */
    stationTagsData: StationTag[] = [];
    stationTagsList: StationTag[] = [];
    original: StationTag[] = [];

    constructor(
        private stationTagService: StationTagService,
    ) {
        super();
    }

    ngOnInit(): void {
        this.commands = [
            new Command("add", "添加", "fa fa-plus", "setofkey:insert", blade => this.exeAdd(blade), blade => this.canExeAdd(blade)),
            new Command("edit", "编辑", "fa fa-edit", "setofkey:update", blade => this.exeEdit(blade), blade => this.canExeEdit(blade)),
            new Command("delete", "删除", "fa fa-trash-o", "setofkey:delete", blade => this.exeDelete(blade), blade => this.canDelete(blade))
        ];
        super.ngOnInit();
    }
	/**
	 * 初始化数据
	 *
	 *
	 * @memberof FarmListComponent
	 */
    initData(): any | Observable<any> | Promise<any> {
        this.stationTagService.getStationTagsList()
            .then(res => {
                if (res) {
                    this.original = [...res];
                };
                this.stationTagsList = [...this.original];
                this.stationTagsData = this.stationTagsList.filter(o => o.farmId == (this.parentEntity as Farm).farmId)
                // this.currentEntity = this.stationTags[0];
                // this.onSeletedChange(this.currentEntity);
            })
            .catch(error => this.bladeData.error = error)
            .then(res => this.bladeData.isLoading = false);
    }

    exeAdd(blend: Blade): void {
        let data: BladeData = new BladeData();
        data.id = "stationTagCreate";
        data.title = "新增关口表";
        //data.subtitle = "新建线路";
        data.isClosingDisabled = false;
        let createStationTagBlade = this.bladeService.createBlade(StationTagEditComponent, data, true);
        this.bladeService.showBlade(createStationTagBlade, this.currentBlade);
    }

    canExeAdd(blend: Blade): boolean {
        return true;
    }

    exeEdit(blend: Blade): void {
        if (this.currentEntity != undefined) {
            let data: BladeData = new BladeData();
            data.id = "stationTagEdit";
            data.title = this.currentEntity.stationTagName;
            let editStationTagBlade = this.bladeService.createBlade(StationTagEditComponent, data, false, this.currentEntity);
            this.currentBlade.bladeService.showBlade(editStationTagBlade, this.currentBlade);
        }
    }

    canExeEdit(blend?: Blade): boolean {
        return this.currentEntity != null;
    }

    exeDelete(blend: Blade): void {
        let selectedStations = this.getSelected();
        if (selectedStations.length > 0) {
            this.dialogService
                .showConfirmationDialog("确认删除", "是否要删除选中项？")
                .subscribe(dialogResult => {
                    if (dialogResult === DialogResult.Ok) {
                        this.currentBlade.bladeData.isLoading = true;
                        this.stationTagService.batchDelete(selectedStations.map(o => o.stationTagId))
                            .then(res => {
                                if (res) {
                                    this.refresh(undefined);
                                }
                            })
                            .catch(error => this.bladeData.error = error)
                            .then(res => this.bladeData.isLoading = false);
                    }
                });

        }
    }

    canDelete(blend?: Blade): boolean {
        return this.getSelected().length > 0;
    }

    onSeletedChange(stationTag: StationTag) {
        if (this.currentBlade.childrenCanDeactivate()) {
            this.currentEntity = stationTag;
        }
    }

	/**
     * 单选
     *
     * @param {any} event
     * @param {any} key
     *
     * @memberof FarmListComponent
     */
    onItemCheack(event: HTMLInputElement, key: StationTag) {
        key.isChecked = event.checked;
    }
    /**
     * 全选/全不选
     *
     * @param {any} event
     *
     * @memberof FarmListComponent
     */
    onAllCheack(event: HTMLInputElement) {
        if (this.stationTagsData.length > 0) {
            for (let i = 0; i < this.stationTagsData.length; i++) {
                this.stationTagsData[i].isChecked = event.checked;
            }
        }
    }

	/**
     * 获得选中项
     *
     * @returns {Setofkey[]}
     *
     * @memberof FarmListComponent
     */
    getSelected(): StationTag[] {
        if (this.stationTagsData.length > 0) {
            let filter = this.stationTagsData.filter(ref => ref.isChecked)
            return filter == null ? [] : filter;
        }
        return [];
    }


}



// WEBPACK FOOTER //
// ./src/app/module/basic/station-tag/station-tag-list/station-tag-list.component.ts
